﻿using System;
using System.Text;

namespace smp.QueryDb
{
    internal class DebugLog : ILog
    {
        public void Debug(string messageFormat, params object[] parameters)
        {
            Write(null, messageFormat, "DEBUG", parameters);
        }

        public void Info(string messageFormat, params object[] parameters)
        {
            Write(null, messageFormat, "INFO", parameters);
        }

        public void Warning(string messageFormat, params object[] parameters)
        {
            Write(null, messageFormat, "WARN", parameters);
        }

        public void Warning(Exception ex, string messageFormat, params object[] parameters)
        {
            Write(ex, messageFormat, "WARN", parameters);
        }

        public void Warning(Exception ex)
        {
            Write(null, null, "WARN", null);
        }

        public void Error(string messageFormat, params object[] parameters)
        {
            Write(null, messageFormat, "ERROR", parameters);
        }

        public void Error(Exception ex, string messageFormat, params object[] parameters)
        {
            Write(ex, messageFormat, "ERROR", parameters);
        }

        public void Error(Exception ex)
        {
            Write(ex, null, "ERROR", null);
        }

        public void Fatal(string messageFormat, params object[] parameters)
        {
            Write(null, messageFormat, "FATAL", parameters);
        }

        public void Fatal(Exception ex, string messageFormat, params object[] parameters)
        {
            Write(ex, messageFormat, "FATAL", parameters);
        }

        public void Fatal(Exception ex)
        {
            Write(ex, null, "FATAL", null);
        }

        private void Write(Exception ex, string messageFormat, string level, params  object[] parameters)
        {
            StringBuilder text = new StringBuilder();
            text.AppendFormat("{0} : ", level);
            if (parameters != null && parameters.Length > 0)
            {
                text.AppendFormat(messageFormat, parameters);
            }
            else
            {
                text.Append(messageFormat);
            }

            if (ex == null)
            {
                System.Diagnostics.Debug.WriteLine(text);
                return;
            }

            if (text.Length > 0)
            {
                text.AppendLine("Excpetion details:");
            }
            SetExceptionText(ex, text);
            System.Diagnostics.Debug.WriteLine(text.ToString());
        }

        private void SetExceptionText(Exception ex, StringBuilder text)
        {
            text.AppendLine("Exception Message : " + ex.Message);
            text.AppendLine("Stack trace : " + ex.StackTrace);
            if (ex.InnerException != null)
            {
                text.AppendLine("Inner Exception :");
                SetExceptionText(ex.InnerException, text);
            }
        }
    }
}
